#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <map> 

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 100010, M = 3 * N;

int n, m;
struct Edge {
    int a, b, w;
    bool used;
    bool operator< (const Edge& W) const {
        return w < W.w;
    }
}edge[M];
int h[N], e[M], ne[M], w[M], idx;
int p[N];
int depth[N], fa[N][17], d1[N][17], d2[N][17];
int q[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

LL kruskal() {
    flr (i, 1, n) p[i] = i;
    sort(edge, edge + m);
    
    LL res = 0;
    flr (i, 0, m - 1) {
        int a = find(edge[i].a), b = find(edge[i].b), w = edge[i].w;
        if (a != b) {
            p[a] = b;
            res += w;
            edge[i].used = true;
        }
    }
    
    return res;
}

void build() {
    mt(h, -1, szf h);
    flr (i, 0, m - 1)
        if (edge[i].used) {
            int a = edge[i].a, b = edge[i].b, w = edge[i].w;
            add(a, b, w), add(b, a, w);
        }
}

void bfs() {
    mt(depth, 0x3f, szf depth);
    depth[0] = 0, depth[1] = 1;
    int hh = 0, tt = 0;
    q[0] = 1;
    
    while (hh <= tt) {
        int t = q[hh ++ ];
        
        fmp (i, t) {
            int j = e[i];
            
            if (depth[j] > depth[t] + 1) {
                depth[j] = depth[t] + 1;
                q[ ++ tt] =j;
                fa[j][0] = t;
                d1[j][0] = w[i], d2[j][0] = -INF;
                
                flr (k, 1, 16) {
                    int anc = fa[j][k - 1];
                    fa[j][k] = fa[anc][k - 1];
                    
                    int distance[4] = {d1[j][k - 1], d2[j][k - 1], d1[anc][k - 1], d2[anc][k - 1]};
                    d1[j][k] = d2[j][k] = - INF;
                    flr (u, 0, 3) {
                        int d = distance[u];
                        if (d > d1[j][k]) d2[j][k] = d1[j][k], d1[j][k] = d;
                        else if (d < d1[j][k] && d > d2[j][k]) d2[j][k] = d;
                    }
                }
            }
        }
    }
}

int lca(int a, int b, int w) {
    static int distance[N * 2];
    int cnt = 0;
    
    if (depth[a] < depth[b]) swap(a, b);
    frl (k, 16, 0)
        if (depth[fa[a][k]] >= depth[b]) {
            distance[cnt ++ ] = d1[a][k];
            distance[cnt ++ ] = d2[a][k];
            a = fa[a][k];
        }
        
    if (a != b) {
        frl (k, 16, 0)
            if (fa[a][k] != fa[b][k]) {
                distance[cnt ++ ] = d1[a][k];
                distance[cnt ++ ] = d2[a][k];
                distance[cnt ++ ] = d1[b][k];
                distance[cnt ++ ] = d2[b][k];
                a = fa[a][k], b = fa[b][k];
            }
            
        distance[cnt ++ ] = d1[a][0];
        distance[cnt ++ ] = d1[b][0];
    }
    
    int dist1 = -INF, dist2 = -INF;
    flr (i, 0, cnt - 1) {
        int d = distance[i];
        if (d > dist1) dist2 = dist1, dist1 = d;
        else if (d < dist1 && d > dist2) dist2 = d;
    }
    
    if (w > dist1) return w - dist1;
    if (w > dist2) return w - dist2;
    return INF;
}

int main() {
	in(n), in(m);
	
	mt(h, -1, szf h);
	flr (i, 0, m - 1) {
	    int a, b, c;
	    in(a), in(b), in(c);
	    edge[i] = {a, b, c};
	}
    
    LL sum = kruskal();
    build();
    bfs();
    
    LL res = 1e18;
    flr (i, 0, m - 1)
        if (!edge[i].used) {
            int a = edge[i].a, b = edge[i].b, w = edge[i].w;
            res = min(res, sum + lca(a, b, w));
        }
    
    outll(res), pn;
    
    return 0;
}